
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BusinessObject;
namespace Autenticazione
{
	public class Atleti : Page
	{
		protected HtmlHead Head1;
		protected HtmlForm form1;
		protected Label msgLoggedUser;
		protected DataGrid dtgAtleta;
		protected void Page_Load(object sender, EventArgs e)
		{
			if (this.Session["EMail"] == null)
			{
				base.Response.Redirect("Autenticazione.aspx");
			}
			this.msgLoggedUser.Text = string.Format("Utente corrente: {0}", this.Session["EMail"].ToString());
			if (!base.IsPostBack)
			{
				this.LoadGrid();
			}
		}
		protected string GetGender(object gender)
		{
			if (int.Parse(gender.ToString()) == 0)
			{
				return "Maschile";
			}
			return "Feminile";
		}
		protected string GetBeltDescription(object idBelt)
		{
			int id = int.Parse(idBelt.ToString());
			return new Cintura
			{
				Id = id
			}.GetBeltDescription();
		}
		protected bool SelectedGender(string selValue, object gender)
		{
			string sGender = gender.ToString();
			return selValue == sGender;
		}
		protected string GetBirthDay(object birthDay)
		{
			return DateTime.Parse(birthDay.ToString()).ToString("dd/MM/yyyy");
		}
		private void LoadGrid()
		{
			BusinessObject.Atleta a = new BusinessObject.Atleta();
			if (this.Session["EMail"] == null)
			{
				return;
			}
			DataTable dt = a.GetTbAtleti(this.Session["Email"].ToString());
			DataTable dtView = dt.Clone();
			for (int i = this.dtgAtleta.CurrentPageIndex * this.dtgAtleta.PageSize; i <= (this.dtgAtleta.CurrentPageIndex + 1) * this.dtgAtleta.PageSize - 1; i++)
			{
				if (i <= dt.Rows.Count - 1)
				{
					dtView.ImportRow(dt.Rows[i]);
				}
			}
			this.dtgAtleta.VirtualItemCount = dt.Rows.Count;
			this.dtgAtleta.DataSource = dtView;
			this.dtgAtleta.DataBind();
		}
		protected void dtgAtleta_Edit(object sender, DataGridCommandEventArgs e)
		{
			this.ClearViewState();
			this.dtgAtleta.EditItemIndex = e.Item.ItemIndex;
			this.LoadGrid();
		}
		protected void dtgAtleta_Delete(object sender, DataGridCommandEventArgs e)
		{
			BusinessObject.Atleta a = new BusinessObject.Atleta();
			if (this.Session["EMail"] == null)
			{
				return;
			}
			a.Id = long.Parse(this.dtgAtleta.DataKeys[e.Item.ItemIndex].ToString());
			a.EMail = this.Session["EMail"].ToString();
			a.DeleteAtleta();
			this.dtgAtleta.EditItemIndex = -1;
			this.LoadGrid();
		}
		protected void dtgAtleta_Cancel(object sender, DataGridCommandEventArgs e)
		{
			this.dtgAtleta.EditItemIndex = -1;
			this.LoadGrid();
		}
		protected void dtgAtleta_ItemDataBound(object sender, DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.EditItem)
			{
				DropDownList ddl = e.Item.Cells[1].FindControl("edit_Gender") as DropDownList;
				if (ddl != null)
				{
					Tools.FillComboGender(ddl);
					ddl.SelectedValue = (e.Item.DataItem as DataRowView).Row["Gender"].ToString();
				}
				DateTime dt = DateTime.Parse((e.Item.DataItem as DataRowView).Row["BirthDay"].ToString());
				string month;
				if (this.ViewState["Month"] == null)
				{
					month = dt.Month.ToString();
				}
				else
				{
					month = this.ViewState["Month"].ToString();
				}
				string year;
				if (this.ViewState["Year"] == null)
				{
					year = dt.Year.ToString();
				}
				else
				{
					year = this.ViewState["Year"].ToString();
				}
				DropDownList ddlYear = e.Item.Cells[2].FindControl("edit_BirthYear") as DropDownList;
				if (ddlYear != null)
				{
					Tools.FillComboYears(ddlYear);
					ddlYear.SelectedValue = year;
				}
				DropDownList ddlMonth = e.Item.Cells[2].FindControl("edit_BirthMonth") as DropDownList;
				if (ddlMonth != null)
				{
					Tools.FillComboMesi(ddlMonth);
					ddlMonth.SelectedValue = month;
				}
				DropDownList ddlDay = e.Item.Cells[2].FindControl("edit_BirthDay") as DropDownList;
				if (ddlDay != null)
				{
					Tools.FillComboDaysInMonthSelected(ddlDay, int.Parse(month), int.Parse(year));
					if (this.ViewState["Month"] == null && this.ViewState["Year"] == null)
					{
						ddlDay.SelectedValue = dt.Day.ToString();
					}
					else
					{
						ddlDay.SelectedValue = "1";
					}
				}
				DropDownList ddlBelt = e.Item.Cells[3].FindControl("edit_Belt") as DropDownList;
				if (ddlBelt != null)
				{
					Cintura belt = new Cintura();
					Tools.FillCombo(ddlBelt, belt.GetTbBelts(), "Id", "Belt");
					if (this.ViewState["Belt"] == null)
					{
						ddlBelt.SelectedValue = (e.Item.DataItem as DataRowView).Row["Belt"].ToString();
					}
					else
					{
						ddlBelt.SelectedValue = this.ViewState["Belt"].ToString();
					}
				}
				DropDownList ddlGrappling = e.Item.Cells[3].FindControl("ddl_Grappling") as DropDownList;
				if (ddlGrappling != null)
				{
					Tools.FillGrapplingLevel(ddlGrappling);
					if (this.ViewState["Grappling"] == null)
					{
						ddlGrappling.SelectedValue = (e.Item.DataItem as DataRowView).Row["GrapplingLevel"].ToString();
						return;
					}
					ddlGrappling.SelectedValue = this.ViewState["Grappling"].ToString();
				}
			}
		}
		protected void dtgAtleta_Update(object sender, DataGridCommandEventArgs e)
		{
			BusinessObject.Atleta a = new BusinessObject.Atleta();
			if (!this.Page.IsValid)
			{
				return;
			}
			a.Id = long.Parse(this.dtgAtleta.DataKeys[e.Item.ItemIndex].ToString());
			TextBox atleta = e.Item.FindControl("edit_Atleta") as TextBox;
			DropDownList gender = e.Item.FindControl("edit_Gender") as DropDownList;
			DropDownList birthYear = e.Item.FindControl("edit_BirthYear") as DropDownList;
			DropDownList birthMonth = e.Item.FindControl("edit_BirthMonth") as DropDownList;
			DropDownList birthDay = e.Item.FindControl("edit_BirthDay") as DropDownList;
			DropDownList belt = e.Item.FindControl("edit_Belt") as DropDownList;
			DropDownList grappling = e.Item.FindControl("ddl_Grappling") as DropDownList;
			TextBox matchFC = e.Item.FindControl("edit_MatchFullContact") as TextBox;
			TextBox matchLK = e.Item.FindControl("edit_MatchLowKick") as TextBox;
			TextBox matchKR = e.Item.FindControl("edit_MatchK1Rules") as TextBox;
			TextBox weight = e.Item.FindControl("edit_Weight") as TextBox;
			if (atleta != null)
			{
				a.FullName = atleta.Text.Replace("'", "''");
			}
			if (gender != null)
			{
				a.Gender = int.Parse(gender.SelectedValue);
			}
			if (birthYear != null && birthMonth != null && birthDay != null)
			{
				a.BirthDayString = string.Format("{0}/{1}/{2}", birthYear.SelectedValue, birthMonth.SelectedValue, birthDay.SelectedValue);
			}
			if (belt != null)
			{
				a.Belt = int.Parse(belt.SelectedValue);
			}
			if (grappling != null)
			{
				a.GrapplingLevel = grappling.SelectedValue;
			}
			if (matchFC != null)
			{
				a.MatchFullContact = int.Parse(matchFC.Text);
			}
			if (matchLK != null)
			{
				a.MatchLowKick = int.Parse(matchLK.Text);
			}
			if (matchKR != null)
			{
				a.MatchK1Rules = int.Parse(matchKR.Text);
			}
			if (weight != null)
			{
				a.Weight = float.Parse(weight.Text);
			}
			if (this.Session["EMail"] == null)
			{
				base.Response.Redirect("Autenticazione.aspx");
			}
			a.Update();
			this.dtgAtleta.EditItemIndex = -1;
			this.LoadGrid();
		}
		protected void dtgAtleta_SelectedIndexChanged(object sender, DataGridPageChangedEventArgs e)
		{
			this.ClearViewState();
			this.dtgAtleta.CurrentPageIndex = e.NewPageIndex;
			this.dtgAtleta.EditItemIndex = -1;
			this.LoadGrid();
		}
		protected void BirthYear_Changed(object sender, EventArgs e)
		{
			this.ViewState.Add("Year", (sender as DropDownList).SelectedValue);
			this.LoadGrid();
		}
		protected void BirthMonth_Changed(object sender, EventArgs e)
		{
			this.ViewState.Add("Month", (sender as DropDownList).SelectedValue);
			this.LoadGrid();
		}
		protected void Belt_Changed(object sender, EventArgs e)
		{
			this.ViewState.Add("Belt", (sender as DropDownList).SelectedValue);
			this.LoadGrid();
		}
		protected void Grappling_Changed(object sender, EventArgs e)
		{
			this.ViewState.Add("Grappling", (sender as DropDownList).SelectedValue);
			this.LoadGrid();
		}
		protected void ClearViewState()
		{
			this.ViewState.Remove("Year");
			this.ViewState.Remove("Month");
			this.ViewState.Remove("Belt");
			this.ViewState.Remove("Grappling");
		}
	}
}
